package commands;

import java.io.IOException;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import util.Utils;
import vo.User;


public class PostUserCommand implements Comando {

	@Override
	public String execute(String[] args) throws IOException{
		if (args.length != 4) {
			return getSintaxe();
		}
		HTable table = new HTable(User.TABLENAME);
		User user = new User();
		String[] array = {""};
		byte[] userid = user.gerarId(array);
		Put put = new Put(userid);
		
		byte[] attribute  = Utils.createStringInBytes(User.CF_ATTRIBUTES);
		byte[] login  = Utils.createStringInBytes(User.USER_LOGIN);
		byte[] email  = Utils.createStringInBytes(User.USER_EMAIL);
		byte[] passwd  = Utils.createStringInBytes(User.USER_PASSWD);
		
		put.add(attribute, login, Bytes.toBytes(args[1]));
		put.add(attribute, passwd, Bytes.toBytes(args[2]));
		put.add(attribute, email, Bytes.toBytes(args[3]));
		
		table.put(put);	
		
		String id = new String(userid);
		return id;
	}

	@Override
	public String getKey() {
		return "-postUser";
	}

	@Override
	public String getSintaxe() {
		return "-postUser login senha email";
	}

}
